* ImeIme Umana, 12/26/2016 
* North Carolina Voting Tabulation Districts Maptile Extension 
* Based on template by Michael Stepner, stepner@mit.edu

program define _maptile_ncvtd 
	syntax , [  geofolder(string) ///
				mergedatabase ///
				map spmapvar(varname) var(varname) binvar(varname) clopt(string) legopt(string) min(string) clbreaks(string) max(string) mapcolors(string) ndfcolor(string) ///
					savegraph(string) replace resolution(string) map_restriction(string) spopt(string) ///
			 ]
	
	if ("`mergedatabase'"!="") {
		novarabbrev merge 1:m ncvtd  ///
			using `"`geofolder'/ncvtd_database.dta"', nogen 
		exit
	}
	
	if ("`map'"!="") {
		spmap `spmapvar' using `"`geofolder'/ncvtd_coords.dta"' `map_restriction', id(_ID) /// 
			`clopt' ///
			`legopt' ///
			legend(pos(7) size(*1.8)) /// 
			fcolor(`mapcolors') ndfcolor(`ndfcolor') ///
			oc(none ...) ndo(black) ///
			os(vthin ...) nds(vthin) ///
			`spopt'

		* Save graph
		if (`"`savegraph'"'!="") __savegraph_maptile, savegraph(`savegraph') resolution(`resolution') `replace'
		
	}
	
end

* Save map to file
cap program drop __savegraph_maptile
program define __savegraph_maptile

	syntax, savegraph(string) resolution(string) [replace]
	
	* check file extension using a regular expression
	if regexm(`"`savegraph'"',"\.[a-zA-Z0-9]+$") local graphextension=regexs(0)
	
	* deal with different filetypes appropriately
	if inlist(`"`graphextension'"',".gph","") graph save `"`savegraph'"', `replace'
	else if inlist(`"`graphextension'"',".ps",".eps") graph export `"`savegraph'"', mag(`=round(100*`resolution')') `replace'
	else if (`"`graphextension'"'==".png") graph export `"`savegraph'"', width(`=round(3200*`resolution')') `replace'
	else if (`"`graphextension'"'==".tif") graph export `"`savegraph'"', width(`=round(1600*`resolution')') `replace'
	else graph export `"`savegraph'"', `replace'

end
